package com.hcss.core.common.mailmgt.dao;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.hcss.core.common.mailmgt.model.SystemMailModel;
import com.hcss.core.mvc.BaseDao;
import com.hcss.core.util.StringUtils;




/**
 * @ClassName: SystemSetting
 * @Description: TODO 系统设置
 * @author lvfang
 * @date 2013-4-16 下午2:56:00
 * 
 */
@Repository("mailSetDao")
public class MailSetDao extends BaseDao {
	private static final String TBLPREFIX = "t_mailinfo";

	public static String table() {
		return TBLPREFIX;
	}

	/**
	 * @Title: findSysModel
	 * @Description: TODO 查询初始化设置
	 * @return
	 * @throws Exception
	 * @throws
	 */
	public SystemMailModel findSysModel() throws Exception {
		String sql = "select * from " + table();
		RowMapper<?> rowMapper = new BeanPropertyRowMapper<SystemMailModel>(
				SystemMailModel.class);
		return (SystemMailModel) jdbcTemplate.queryForObject(sql, rowMapper);
	}

	/**
	 * 系统设置
	 * 
	 * @param systemModel
	 *            输入参数
	 * @return 提示信息
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	public int setInfo(final SystemMailModel systemModel) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("update ");
		sql.append(table());
		sql.append(" set mailServerHost = ?,mailServerPort=?,mailAdminAddress=?,mailAdminPwd=?,validateInt=?");
		Object[] params = new Object[] { systemModel.getMailServerHost(),
				systemModel.getMailServerPort(),
				systemModel.getMailAdminAddress(),
				systemModel.getMailAdminPwd(),
				systemModel.getValidateInt() };
		if (systemModel != null) {
			return jdbcTemplate.update(sql.toString(), params);
		}
		return 0;
	}
	
	/**
	 * @Title: updateMailTemplate
	 * <p>修改邮件模板<p>
	 * @param systemModel
	 * @return
	 * @return: int
	 */
	@SuppressWarnings("unused")
	public int updateMailTemplate(SystemMailModel systemModel){
		StringBuffer sql = new StringBuffer();
		sql.append("update ");
		sql.append(table());
		Object[] params = null;
		if(!StringUtils.isEmpty(systemModel.getSrtMailTemplate())){
			sql.append(" set srtMailTemplate = ?");
			params = new Object[] {
				systemModel.getSrtMailTemplate()
			};
		}
		if(!StringUtils.isEmpty(systemModel.getSrResponseMailTemplate())){
			sql.append(" set srResponseMailTemplate = ?");
			params = new Object[] {
				systemModel.getSrResponseMailTemplate()
			};
		}
		if(!StringUtils.isEmpty(systemModel.getSrFeedBackMailTemplat())){
			sql.append(" set srFeedBackMailTemplat = ?");
			params = new Object[] {
				systemModel.getSrFeedBackMailTemplat()
			};
		}
		if(!StringUtils.isEmpty(systemModel.getUserLockMailTemplate())){
			sql.append(" set userLockMailTemplate = ?");
			params = new Object[] {
				systemModel.getUserLockMailTemplate()
			};
		}
		if(!StringUtils.isEmpty(systemModel.getCaseWillCloseMailTemplate())){
			sql.append(" set caseWillCloseMailTemplate = ?");
			params = new Object[] {
				systemModel.getCaseWillCloseMailTemplate()
			};
		}
		if (systemModel != null) {
			return jdbcTemplate.update(sql.toString(), params);
		} 
		return 0;
	}
}
